package com.googlecode.neuron.client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class UdpClient {

	public static final int PORT = 4568;
	public static InetAddress  inet_address = null;
	public static void main(String[] args) throws IOException {
		new UdpClient();
	}
	public static ReceiveMessage message;
	public UdpClient() throws IOException{
		inet_address = InetAddress.getByName("192.168.90.25");
		DatagramSocket udp_socket;
		udp_socket = new DatagramSocket(PORT, InetAddress.getLocalHost());
		String buf = inet_address.toString() + ":" + PORT;
		byte[] buffer = buf.getBytes();
		DatagramPacket packet = new DatagramPacket(buffer, buffer.length, inet_address, 2345);
		udp_socket.send(packet);
		message = new ReceiveMessage(udp_socket);
		try {
			Thread.sleep(30 * 1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public UdpClient(InetAddress inet, int port) {
		new ReceiveMessage_(inet, port);
	}
	
	public static void start_thread(Thread old_, InetAddress inet_, int port_) {
		new UdpClient(inet_, port_);
		System.out.println("开启线程成功");
		message.destroy();
		System.out.println("成功结束！！！");
	}
}

class ReceiveMessage_ extends Thread {
	private DatagramSocket udp_socket = null;
	private boolean isRun = true;
	private InetAddress inet_ ;
	private int port_ ;
	public ReceiveMessage_ (InetAddress inet_, int port_) {
		this.inet_ = inet_;
		this.port_ = port_;
		this.start();
	}
	
	public void run() {
		try {
			Thread.sleep(20 * 1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
		}
		try {
			udp_socket = new DatagramSocket(UdpClient.PORT, InetAddress.getLocalHost());
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			System.out.println(e1.getMessage());
		}
		String buf = "Hello !";
		byte[] buffer = buf.getBytes();
		DatagramPacket packet = new DatagramPacket(buffer, buffer.length, inet_, port_);
		try {
			udp_socket.send(packet);
			Thread.sleep(200);
			udp_socket.send(packet);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		
		
		
		System.out.println("开起接听消息线程");
		byte[] buffer_ = new byte[1024];
		DatagramPacket packet_ = new DatagramPacket(buffer_, buffer_.length);
		while(isRun) {
			try {
				udp_socket.receive(packet_);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println(e.getMessage());
			}
			String msg = new String(packet_.getData(), 0, packet_.getLength());
			System.out.println("message is " + msg + "\tport is" + packet_.getPort());
		}
	}
}

class ReceiveMessage extends Thread {
	private DatagramSocket udp_socket = null;
	private boolean isRun = true;
	public ReceiveMessage(DatagramSocket socket) {
		udp_socket = socket;
		this.start();
	}
	
	
	public void run() {
		System.out.println("开起接听消息线程");
		byte[] buffer = new byte[1024];
		DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
		while(isRun) {
			try {
				udp_socket.receive(packet);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println(e.getMessage());
			}
			String msg = new String(packet.getData(), 0, packet.getLength());
			
			if (msg.indexOf("<connect>") != -1) {
				String str = new String(packet.getData(), 0, packet.getLength());
				System.out.println("message:" + str);
				String s[] = str.split(":");
				InetAddress inet_ = null;
				int port_ = -1;
				try {
					String ss = new String(s[1].getBytes(), 1, s[1].length() - 1);
					System.out.println(ss);
					inet_ = InetAddress.getByName(ss);
					port_ = Integer.parseInt(s[2]);
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					System.out.println(e.getMessage());
					System.exit(0);
				}
				isRun = false;
				/*//new Client(inet_, port_);
				System.out.println("over!!!");
				this.destroy();*/
				UdpClient.start_thread(this, inet_, port_);
				break;
			}
			System.out.println("message is " + msg);
		}
	}


	public void destroy() {
		// TODO Auto-generated method stub
		udp_socket.close();
		System.out.println("关闭UDP_Socket成功");
	}
}